<!DOCTYPE html>
<html lang="zh-cn"><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/main.css" rel="stylesheet">

    <link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico" />

    <title>大数的阶乘 | wiseAI的小站</title>
</head><body><div class="container-fluid">
    <nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">
                <img src="/img/favicon.ico" alt="" width="30" height="24" class="d-inline-block align-text-top">
                wiseAI的小站
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/articles/">文章</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/categories/">分类</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/tags/">关键字</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/post/golang/">golang编程</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/about/">关于</a>
                    </li>
                    
                </ul>
                <form class="d-flex">
                    <input id="search-query" class="form-control me-2" type="search" placeholder="Search for anything..." aria-label="Search">
                </form>
            </div>
        </div>
    </nav>
</div>




<div id="content">
<div class="container article">
	<h1>大数的阶乘</h1>
	<div class="post-meta">
		<div>
			
			
			By wiseai | 2022-06-27 | 2 minutes
		</div>
		<div class="tags">
			
			<a class="btn btn-light links btn-sm" href="/tags/%E7%BC%96%E7%A8%8B/">编程</a>
			
			<a class="btn btn-light links btn-sm" href="/tags/c%E8%AF%AD%E8%A8%80/">c语言</a>
			
			<a class="btn btn-light links btn-sm" href="/tags/golang/">golang</a>
			
		</div>
	</div>
	<div>
		<div class="article-post">
			<p>直接上代码:</p>
<h2 id="c语言">C语言:</h2>
<pre tabindex="0"><code>#include &lt;stdio.h&gt;
#include &lt;time.h&gt;

//给计算结果一个数组，并指定大小为4096
//第一次写程序使用的字符串保存大数，但是有个问题，需要char到int来回转换，影响计算性能。
//所以使用数组来保存数据，方便计算
int jieguo[40960] = {0};

//数组长度
int len = 0;

//fac为计算阶乘的函数，计算结果存储在jieguo[4096]数组中
int fac(int n)
{
	int v = 0;
	int f = 0;

	//初始化jieguo数组和数组长度
	jieguo[0] = 1;
	len = 1;

	for(int i = 1; i &lt;= n; i++)
	{
		for(int j = len - 1; j &gt;= 0; j--)
		{
			v = i * jieguo[j];
			jieguo[j] = v % 10;
			v = v / 10;

			f = j + 1;

			//如果v大于0，说明还要进位，所以将进位的数加上原来的数，判断进位后存储。
			//为了提高效率，使用for进行循环
			for(; v &gt; 0 &amp;&amp; f &lt; len; f++)
			{
				v = v + jieguo[f];
				jieguo[f] = v % 10;
				v = v / 10;
			}
			
			//如果这时f等于数组的长度len并且v还大于0，说明需要增加jieguo数组的长度
			//为了提高效率，仍然使用for进行循环
			for(; v &gt; 0;)
			{
				jieguo[len] = v % 10;
				v = v / 10;
				len++;
				
				if(len &gt; 40960 - 2)
				{
					//这个时候说明数组长度不够，需要增加jieguo后面的数值
					//计算100的阶乘的数组长度为158，计算1000的阶乘的数组长度为2568。
					//可以根据实际需要进行设置。
					return 1;
				}
			}
		}		
	}
	return 0;
}

//下面写主函数
int main(void)
{
	int num;
	printf(&#34;请输入需要计算阶乘的数字:&#34;);
	scanf(&#34;%d&#34;, &amp;num);
	
	//计算阶乘结果
	int r = fac(num);
	//如果返回1,则说明数组长度不够，需要增加长度
	if(r == 1)
	{
		printf(&#34;计算结果太大，请增加数组长度！\n&#34;);
		return 0;
	}
	printf(&#34;%d的阶乘为：&#34;, num);
	for(int i = len - 1; i &gt;= 0; i--)
	{
		printf(&#34;%d&#34;, jieguo[i]);
	}
	printf(&#34;\n&#34;);
	printf(&#34;%d\n&#34;, len);
	printf(&#34;运行时间:%.2lf秒\n&#34;, (double)clock() / CLOCKS_PER_SEC);
	printf(&#34;\n&#34;);
	return 0;	
}
</code></pre><h2 id="golang">golang:</h2>
<pre tabindex="0"><code>package main

import &#34;fmt&#34;
import &#34;time&#34;

const m int = 40960

func fac(n int) ([m]int, int) {
	var jieguo [m]int
	var len int
	var v, f = 0, 0

	jieguo[0] = 1
	len = 1
	
	for i := 1; i &lt;= n; i++{
		for j := len - 1; j &gt;= 0; j--{
			v = i * jieguo[j]
			jieguo[j] = v % 10
			v = v / 10 
			
			f = j + 1

			for ; v &gt; 0 &amp;&amp; f &lt; len; f++ {
				v = v + jieguo[f]
				jieguo[f] = v % 10
				v = v / 10
			}

			for ; v &gt; 0; len++ {
				jieguo[len] = v % 10
				v = v / 10
				
				if len &gt; m - 2 {
					fmt.Println(&#34;error!&#34;)
					return jieguo, len
				}
			}
		}
	}
	return jieguo, len
}

func main() {
	a := 10000
	startTime := time.Now()
	f, len := fac(a)
	fmt.Printf(&#34;%d的阶乘为：&#34;, a)
	for i := len - 1; i &gt;= 0; i-- {
		fmt.Print(f[i])
	}
	fmt.Print(&#34;\n&#34;)
	fmt.Println(len)
	
	t := time.Since(startTime)
    fmt.Println(t.Seconds())
}
</code></pre>
		</div>
	</div>
</div>


<div class="container">
    
    <div class="row">
        
        <div class="col-5">
            <a class="page-link link-dark text-end dh" href="/post/linux%E4%B8%ADfirefox%E6%94%AF%E6%8C%81flash/">
                <h5>前一篇</h5><br>
                Linux中firefox支持flash
            </a>            
        </div>
        
        <div class="col-2">
        </div>
        
        <div class="col-5">
            <a class="page-link link-dark text-start dh" href="/post/%E6%84%81/">
                <h5>后一篇</h5><br>
                愁
            </a>            
        </div>
        
    </div>
    
</div>

        </div><br><br>
<footer class="container">
    <h2>友情链接</h2>
    <hr>
    <nav class="nav nav-pills flex-column flex-sm-row">
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/gnzg/index.html" target="_blank">孤鸟之歌</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/mm/index.html" target="_blank">生成随机字符</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/md/index.html" target="_blank">MarkDown</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/" target="_blank">Gitee</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://github.com/" target="_blank">Github</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyun.com/" target="_blank">阿里云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cloud.tencent.com/" target="_blank">腾讯云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.oschina.net/" target="_blank">OSCHINA开源中国</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/wiseai/the-way-to-go_ZH_CN/blob/master/eBook/directory.md" target="_blank">the way to go</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://topgoer.com/" target="_blank">golang文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://goframe.org/display/gf" target="_blank">GoFrame</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyundrive.com/" target="_blank">阿里云盘</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cn.vuejs.org/" target="_blank">vue3文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://element-plus.gitee.io/zh-CN/" target="_blank">element-plus文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.runoob.com/vue3/vue3-tutorial.html" target="_blank">vue3菜鸟教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://v5.bootcss.com/" target="_blank">bootstrap v5 中文文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.bootstrap.cn/" target="_blank">bootstrap文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://caddy2.dengxiaolong.com/docs/" target="_blank">caddy中文教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.58pic.com/" target="_blank">千图</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://ifonts.com/" target="_blank">iFonts</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://marketing.qiniu.com/" target="_blank">七牛云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.ixigua.com/" target="_blank">西瓜视频</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/yugang/index.html" target="_blank">鱼缸计算</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://justcc.mengkang.net/#/" target="_blank">C语言JustCC</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/pptist/" target="_blank">PPTist</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="/index.xml" target="_blank">RSS</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.kancloud.cn/idcpj/python/418553" target="_blank">参考资料</a>
        
    </nav>

    <div class="copyright text-center">
      <span class="power-by">
        Powered by <a class="links" href="https://gohugo.io" target="_blank">Hugo</a>
    </span>
    <span>|</span>
    <span>
        Theme - <a class="links" href="https://github.com/wiseai-go/blog-hugo" target="_blank">WiseAI</a>
    </span>
    <br>
    <span class="copyright-year">
        &copy;
        
        2017 -
        2023<span>
            陇ICP备15000157号
            
        </span></span>

</div>
</footer>
<script src="/js/bootstrap.bundle.min.js"></script>


</body>
</html>
